$(function() {
    $( ".selector" ).datepicker( "option", "dateFormat", 'dd-mm-yy' );
    var dates = $( "#from, #to" ).datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 3,
        onSelect: function( selectedDate ) {
            var option = this.id == "from" ? "minDate" : "maxDate",
            instance = $( this ).data( "datepicker" ),
            date = $.datepicker.parseDate(
                instance.settings.dateFormat ||
                $.datepicker._defaults.dateFormat,
                selectedDate, instance.settings );
            dates.not( this ).datepicker( "option", option, date );
        }
    });
});
        
var refreshChart = function() {
    render = document.getElementById("mainForm:Render").getAttribute("value");
    if(render != 'render')
        return false;
    
    document.getElementById("mainForm:Render").setAttribute("value", "not");
    
    eval(document.getElementById("mainForm:EletricUnit").getAttribute("value"));
    eval(document.getElementById("mainForm:MoneyUnit").getAttribute("value"));
    eval(document.getElementById("mainForm:UserName").getAttribute("value"));
            
    eval(document.getElementById("mainForm:EletricData").getAttribute("value"));
    eval(document.getElementById("mainForm:MoneyData").getAttribute("value"));
    eval(document.getElementById("mainForm:SecMoneyData").getAttribute("value"));
    
    eval(document.getElementById("mainForm:Annotations").getAttribute("value"));
    
    var s = Stats();
            
    var horaRadio = document.getElementById("mainForm:calRadioId:0");
    var diaRadio = document.getElementById("mainForm:calRadioId:1");
    var mesRadio = document.getElementById("mainForm:calRadioId:2");
    var semanaRadio = document.getElementById("mainForm:calRadioId:3");
            
    if(horaRadio.checked){
        s.drawChart(null, eletricUnit, userName, eletricData, null, ['#4572A7'],  3600 * 1000, 0, 'eletric');
        s.drawChart(null, moneyUnit, userName, moneyData, secMoneyData, ['#8C9566', '#4572A7'], 3600 * 1000, 0, 'money');
    } else if(diaRadio.checked){
        s.drawChart(null, eletricUnit, userName, eletricData, null, ['#4572A7'], 24 * 3600 * 1000,1, 'eletric');
        s.drawChart(null, moneyUnit, userName, moneyData, secMoneyData, ['#8C9566', '#4572A7'], 24 * 3600 * 1000, 1,'money');
    } else if(mesRadio.checked){
        s.drawChart(null, eletricUnit, userName, eletricData, null, ['#4572A7'], 30 * 24 * 3600 * 1000, 2, 'eletric');
        s.drawChart(null, moneyUnit, userName, moneyData, secMoneyData, ['#8C9566', '#4572A7'], 30 * 24 * 3600 * 1000, 2, 'money');
    }else if(semanaRadio.checked){
        var xlist = ['Domingo', 'Segunda', 'Terça', 'Quarta', 'Quinta', 'Sexta', 'Sabado'];
        s.drawChart(xlist, eletricUnit, userName, eletricData, null, ['#4572A7'], 0, 3, 'eletric');
        s.drawChart(xlist, moneyUnit, userName, moneyData, secMoneyData, ['#8C9566', '#4572A7'], 0, 3, 'money');
    } else {
        s.drawChart(null, eletricUnit, userName, eletricData, null, ['#4572A7'],  3600 * 1000, 0, 'eletric');
        s.drawChart(null, moneyUnit, userName, moneyData, secMoneyData, ['#8C9566', '#4572A7'], 3600 * 1000, 0, 'money');
    }
       
    
    $('#mainForm\\:calSubmitButton').tipsy({
        gravity: 'w', 
        fade: true, 
        delayIn: 100, 
        delayOut: 300
    });
    $('#editCalLink').tipsy({
        gravity: 'sw', 
        fade: true, 
        delayIn: 100, 
        delayOut: 300
    });
    document.getElementById('mainForm:calRadioId:0').setAttribute("title", "Selecione para ver as estatísticas com um intervalo de uma hora entre registos.")
    $('#mainForm\\:calRadioId\\:0').tipsy({
        gravity: 'nw', 
        fade: true, 
        delayIn: 100, 
        delayOut: 300
    });
    document.getElementById('mainForm:calRadioId:1').setAttribute("title", "Selecione para ver as estatísticas com um intervalo de um dia entre registos.")
    $('#mainForm\\:calRadioId\\:1').tipsy({
        gravity: 'n', 
        fade: true, 
        delayIn: 100, 
        delayOut: 300
    });
    document.getElementById('mainForm:calRadioId:2').setAttribute("title", "Selecione para ver as estatísticas com um intervalo de um mês entre registos.")
    $('#mainForm\\:calRadioId\\:2').tipsy({
        gravity: 'n', 
        fade: true, 
        delayIn: 100, 
        delayOut: 300
    });
    document.getElementById('mainForm:calRadioId:3').setAttribute("title", "Selecione para ver a média dos gastos por dia de semana.")
    $('#mainForm\\:calRadioId\\:3').tipsy({
        gravity: 'ne', 
        fade: true, 
        delayIn: 100, 
        delayOut: 300
    });
    $(function() {
        $('#contractSelector [title]').tipsy({
            trigger: 'focus', 
            gravity: 'se'
        });
    });
    $(function() {
        $('#tariffSelector [title]').tipsy({
            trigger: 'focus', 
            gravity: 'e'
        });
    });
       
    return false;
};